<?php	
	$a = REPLACE_Injection($_GET["a"]);
	if($a == 1){
		$id = REPLACE_Injection($_GET["id"]);
		?>
		<div id="all-tournament">		
		<?php mostrarComplaints($id);?>
		</div><?php
	}elseif($a == 2){
		$id = REPLACE_Injection($_GET["id"]);
		if(!empty($_POST)){						
			$ok = NULL;
			$error = NULL;
			$image = "";
			$video = "";
			$text = utf8_encode(strip_tags(REPLACE_Injection($_POST["complaint"])));
			$partido = REPLACE_Injection($_POST["match"]);
			
			if(!$partido){					
				$error = "An error has ocurred, no match selected";
			}else{			
				if(strlen($text) > 0){
					$values = array(NULL, $partido, $_SESSION["user_id"], "", $text, 0, $video, 1, $id, time(), time(), 0);
					$insert = new mysqlInsert("tournament", "", "complaint_partido", $values);
					$insert->execute();
					$complaintId = $insert->getInsertedId();
					$img = $complaintId;
					$imgId = processImage(	IMG_MAX_ALLOWED_SIZE, 
																IMG_PATH.IMG_COMPLAINTS_PATH, 
																IMG_PATH.IMG_COMPLAINTS_PATH.IMG_THUMBNAIL_PATH, 
																IMG_THUMBNAIL_WIDTH, 
																$img, 
																"picture", 
																NULL);
					
					if($imgId != 0){
						if($imgId > 0){
							$fields = array("imagen");
							$values = array($imgId);
							$update = new mysqlUpdate("tournament", "", "complaint_partido", $fields, $values, "`id`='".$complaintId."'");
							$update->execute();
						}else{
							$delete = new mysqlDelete("tournament", "", "complaint_partido", "`id`='".$id."'");
							$delete->execute();
							
							if($imgId == -1)
								$error = "The uploaded picture has a not valid format. Use only JPG or PNG files";
							elseif($imgId == -2)
								$error = "An unexpected error has ocurred, please try again later or post a bug topic in the correct forum";
						}
					}
	
					if(empty($error)){				
						$u = "UPDATE complaint_partido SET `estado`='1', `last_fecha`='".time()."', responses = responses +1 WHERE `id`='".$id."'";				
						$update = new mysqlUpdate("tournament", $u, "", NULL, NULL, "");
						$update->execute();
						$ok = "Complaint Sent!";
					}
				}else{
					$error = "You have to fill the text field";
				}
			}
		}		
		?>
		<div id="all-tournament">		
		<?php mostrarComplaints($id, $ok, $error);?>
		</div><?php
	}elseif($a == 3){
		$id = REPLACE_Injection($_GET["id"]);
		$father = REPLACE_Injection($_GET["father"]);
		$delete = new mysqlDelete("tournament", "", "complaint_partido", "`id`='".$id."' AND `padre`='".$father."'");
		$delete->execute();					
		?>
		<div id="all-tournament">		
		<?php mostrarComplaints($father, "Complain deleted");?>
		</div><?php
	}else{
		?>
		<div id="content">
		<h1>Complaints Management</h1>		
		<?php
		$q = "SELECT 	c.id as id, 
								u.usuario as usuario, 
								u.jugador as jugador,				 
								c.estado as estado, 
								c.fecha as fecha, 
								c.last_fecha as last_fecha, 
								c.acusado as acusado, 
								c.responses as total_responses
					FROM complaint_partido as c, usuarios as u 
					WHERE c.usuario = u.id AND c.padre = '0' AND c.estado != '3' ORDER BY last_fecha DESC";
		$query = new mysqlQuery("tournament", $q, "", "", "", "", "");
		$query->execute();
		if($query->getCount() > 0){
			?>
			<table border="0" cellspacing="0" cellpadding="0" class="table">
			<tr>
				<td class="title">Creation Date</td>
				<td class="title">Last Updated</td>
				<td class="title">Author</td>
				<td class="title">Complains To</td>
				<td class="title">Messages</td>
				<td class="title">Status</td>
				<td class="title">&nbsp;</td>
			</tr>
			<?php
			$n = 0;
			while($row = $query->next()){
				$status = obtenerEstadoComplainTexto($row["estado"]);
				echo '<tr><td class="td'.(($n%2)+1).'">'.date("d/m/Y h:m", $row["fecha"]).'</td>';
				echo '<td class="td'.(($n%2)+1).'">'.date("d/m/Y h:m", $row["last_fecha"]).'</td>';
				echo '<td class="td'.(($n%2)+1).'"><strong>'.$row["jugador"].' ('.$row["usuario"].')</strong></td>';
				echo '<td class="td'.(($n%2)+1).'"><strong>'.$row["acusado"].'</strong></td>';
				echo '<td class="td'.(($n%2)+1).'">'.($row["total_responses"]+1).'</td>';
				echo '<td class="td'.(($n%2)+1).'">'.$status.'</td>';
				echo '<td class="td'.(($n%2)+1).'"><a href="index.php?s=complaints-management&a=1&id='.$row["id"].'">Open Complaint</a></td></tr>';
				$n ++;
			}
			?>
			</table>
			<?php
		}else{
			?>
			<div id="error">Any complaint sent</div>
			<?php
		}
		?></div><?php
	}
?>
</div>